package com.example.wweather;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class PlaceHandler {
	ArrayList<Lugar> xPlace = new ArrayList<Lugar>();

	public PlaceHandler() {
	
	}

	public PlaceHandler(String u) {

	}

	public void startParsing(JSONObject gPlaces) {
		JSONObject json;

		json = gPlaces;
		try {
			JSONArray places = json.getJSONArray("results");
			for (int i = 0; i < places.length(); i++) {
				JSONObject w = places.getJSONObject(i);
				Lugar place = new Lugar();

				place.setNombre(w.getString("name"));
				place.setDireccion(w.getString("vicinity"));
				
				if(w.has("price_level")){

				place.setPrecio(Integer.parseInt(w.getString("price_level")));}

				JSONObject desc = w.getJSONObject("geometry");
				JSONObject loc = desc.getJSONObject("location");
				place.setLat(Double.parseDouble(loc.getString("lat")));
				place.setLon(Double.parseDouble(loc.getString("lng")));
				
				JSONArray tips = w.getJSONArray("types");
				place.setTipo(tips.get(1).toString());
				xPlace.add(place);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
    public ArrayList<Lugar> getPlaces() {
        return xPlace;
    }

    public void setPlaces(ArrayList<Lugar> places) {
        this.xPlace = places;
    }

}
